package cn.ecnu.controller;

import cn.ecnu.common.utils.JwtToken;
import cn.ecnu.common.utils.R;
import cn.ecnu.mapper.UserMapper;
import cn.ecnu.mapper.VisitorMapper;
import cn.ecnu.pojo.User;
import cn.ecnu.pojo.Visitor;
import cn.ecnu.service.VisitorService;
import cn.ecnu.vo.Converter;
import cn.ecnu.vo.fromFront.CommentConsultVo;
import cn.ecnu.vo.fromFront.EndConsultVo;
import cn.ecnu.vo.fromFront.PersonalInfoForm;
import cn.ecnu.vo.fromFront.StartConsultVo;
import cn.ecnu.vo.toFront.VisitorVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/visitor")
public class VisitorController {

    @Resource
    private VisitorMapper visitorMapper;

    @Resource
    private VisitorService visitorService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private Converter converter;

    @GetMapping("/getAllVisitors")
    public R getAllVisitors() {
        try {
            List<Visitor> visitors = visitorMapper.getAllVisitors();
            ArrayList<VisitorVo> result = new ArrayList<>();
            for (Visitor visitor : visitors) {
                result.add(converter.getVisitorVo(visitor.getVisitorId()));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getVisitorById/{id}")
    public R getVisitorById(@PathVariable("id") Integer id) {
        try {
            VisitorVo vo = converter.getVisitorVo(id);
            return R.ok().put("data", vo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/ban/{id}")
    public R ban(@PathVariable("id") Integer id) {
        try {
            visitorMapper.banVisitorById(id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    // 访客发起咨询
    @JwtToken(required = false)
    @PostMapping("/startConsult")
    public R startConsult(@RequestBody StartConsultVo vo) {
        return R.ok().put("data", visitorService.startConsult(vo));
    }

    // 访客结束咨询
    @JwtToken(required = false)
    @PostMapping("/endConsult")
    public R endConsult(@RequestBody EndConsultVo vo) {
        visitorService.endConsult(vo);
        return R.ok();
    }

    // 访客取消咨询
    @JwtToken(required = false)
    @GetMapping("/cancelConsult/{id}")
    public R cancelConsult(@PathVariable("id") Integer id) {
        visitorService.cancelConsult(id);
        return R.ok();
    }

    // 访客评价某次咨询
    @JwtToken(required = false)
    @PostMapping("/commentConsult")
    public R commentConsult(@RequestBody CommentConsultVo vo) {
        visitorService.commentConsult(vo);
        return R.ok();
    }

    @PostMapping("/modifyPersonalInfo")
    public R modifyPersonalInfo(@RequestBody PersonalInfoForm vo) {
        try {

            Integer id = vo.getId();

            //user
            User user = userMapper.getUserById(id);
            user.setUserName(vo.getName());
            user.setUserPhone(vo.getPhone());
            userMapper.updateUser(user);

            //visitor
            Visitor visitor = visitorMapper.getVisitorById(id);
            visitor.setLastLoginTime(new Date());
            visitor.setEmergencyName(vo.getEmergencyName());
            visitor.setEmergencyPhone(vo.getEmergencyPhone());
            visitorMapper.updateVisitor(visitor);

            return R.ok();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

}
